<template>
  <div class="admin-login-container">
    <div class="admin-login-box">
      <div class="admin-login-header">
        <h2>管理员登录</h2>
        <p>请输入管理员账号和密码</p>
      </div>


      <el-form
        ref="adminLoginFormRef"
        :model="adminLoginForm"
        :rules="adminLoginRules"
        class="admin-login-form"
        @submit.prevent="handleAdminLogin"
      >
        <el-form-item prop="username">
          <el-input
            v-model="adminLoginForm.username"
            placeholder="管理员用户名"
            prefix-icon="User"
            size="large"
          />
        </el-form-item>
        
        <el-form-item prop="password">
          <el-input
            v-model="adminLoginForm.password"
            type="password"
            placeholder="管理员密码"
            prefix-icon="Lock"
            size="large"
            show-password
          />
        </el-form-item>
        
        <el-form-item>
          <el-button
            type="primary"
            size="large"
            :loading="loading"
            @click="handleAdminLogin"
            style="width: 100%"
          >
            管理员登录
          </el-button>
        </el-form-item>
      </el-form>
      
      <div class="admin-login-footer">
        <el-button type="text" @click="goToUserLogin">返回用户登录</el-button>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref, reactive } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { User, Lock } from '@element-plus/icons-vue'

const router = useRouter()

// 响应式数据
const loading = ref(false)
const adminLoginFormRef = ref()

// 管理员登录表单
const adminLoginForm = reactive({
  username: 'admin',
  password: '123456'
})

// 管理员登录验证规则
const adminLoginRules = {
  username: [
    { required: true, message: '请输入管理员用户名', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入管理员密码', trigger: 'blur' },
    { min: 6, message: '密码长度不能少于6位', trigger: 'blur' }
  ]
}

// 管理员登录方法
const handleAdminLogin = async () => {
  try {
    await adminLoginFormRef.value.validate()
    loading.value = true
    
    console.log('开始管理员登录验证...')
    
    // 验证管理员账号密码
    if (adminLoginForm.username === 'admin' && adminLoginForm.password === '123456') {
      console.log('管理员账号密码验证成功')
      
      // 保存管理员信息
      localStorage.setItem('token', 'admin-token')
      localStorage.setItem('userInfo', JSON.stringify({
        username: 'admin',
        role: 'admin'
      }))
      
      // 验证信息是否保存成功
      const savedToken = localStorage.getItem('token')
      const savedUserInfo = JSON.parse(localStorage.getItem('userInfo') || '{}')
      
      console.log('管理员登录信息保存:', {
        token: savedToken,
        userInfo: savedUserInfo
      })
      
      if (savedToken && savedUserInfo.role === 'admin') {
        ElMessage.success('管理员登录成功')
        
        // 等待一下确保 localStorage 写入完成
        await new Promise(resolve => setTimeout(resolve, 100))
        
        // 使用 window.location 直接跳转，避免路由守卫的时序问题
        window.location.href = '/admin/dashboard'
      } else {
        throw new Error('管理员信息保存失败')
      }
    } else {
      ElMessage.error('管理员账号或密码错误')
    }
  } catch (error) {
    console.error('管理员登录失败:', error)
    ElMessage.error('管理员登录失败，请重试')
  } finally {
    loading.value = false
  }
}

// 返回用户登录页面
const goToUserLogin = () => {
  router.push('/login')
}
</script>

<style scoped>
.admin-login-container {
  min-height: 100vh;
  background: linear-gradient(135deg, #2c3e50 0%, #34495e 100%);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.admin-login-box {
  background: white;
  border-radius: 15px;
  padding: 40px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.3);
}

.admin-login-header {
  text-align: center;
  margin-bottom: 30px;
}

.admin-login-header h2 {
  color: #2c3e50;
  margin-bottom: 10px;
  font-size: 1.8em;
}

.admin-login-header p {
  color: #7f8c8d;
  font-size: 14px;
}

.admin-login-form {
  margin-bottom: 20px;
}

.admin-login-footer {
  text-align: center;
  color: #7f8c8d;
  font-size: 14px;
}

:deep(.el-input__wrapper) {
  border-radius: 8px;
}

:deep(.el-button--primary) {
  border-radius: 8px;
  height: 45px;
  font-size: 16px;
  background: #2c3e50;
  border-color: #2c3e50;
}

:deep(.el-button--primary:hover) {
  background: #34495e;
  border-color: #34495e;
}
</style> 